import 'dart:convert';
import 'package:flutter/widgets.dart';
import '../utils/common.dart';

/// String 扩展
extension StringExt on String? {

  /// 转换为对象
  toJson() {
    if (this == null) return null;
    return jsonDecode(this!);
  }

  int? toInt() {
    if (this == null) return null;
    return int.parse(this!);
  }

  /// 关键字自定义样式字富文本
  /// keyword 关键字
  /// textColor 文本颜色
  /// fontSize 文本大小
  /// fontWeight 文本样式
  List<TextSpan>? keywordSpan({
    required String keyword,
    Color? textColor,
    double? fontSize,
    FontWeight? fontWeight,
  }) {
    List<TextSpan> list = [];
    if (this == null) return list;
    TextStyle keywordTextStyle = TextStyle(
      color: textColor,
      fontSize: fontSize,
      fontWeight: fontWeight,
    );
    String match = "[${keyword.split("").join(",")}]";
    this!.split("").forEach((element) {
      if (element.contains(RegExp(match))) {
        list.add(TextSpan(text: element, style: keywordTextStyle));
      } else {
        list.add(TextSpan(text: element));
      }
    });
    return list;
  }

  /// 最大字符
  String? maxLength({
    int? length,
    String placeholder = "...",
  }) {
    if (this == null) return null;
    String str = this!;
    if (length != null && str.length > length) {
      str = str.substring(0, length) + placeholder;
    }
    return str;
  }

  /// String修饰
  /// prefix 前缀
  /// suffix 后缀
  /// placeholder 占位符
  ///
  String toDecorate({
    String prefix = "",
    String suffix = "",
    String placeholder = "--",
  }) {
    if (isEmptyOrNull(this)) {
      return placeholder;
    }
    return prefix + this! + suffix;
  }

  ///判断两个字符串是否相等
  ///other 另外一个对比的字符串
  ///ignoreCase 是否忽略打小写
  bool equal(String other, {bool ignoreCase = false}) {
    if (this == null) {
      return false;
    }
    if (ignoreCase) {
      return this!.toUpperCase().compareTo(other.toUpperCase()) == 0;
    } else {
      return this!.compareTo(other) == 0;
    }
  }

  /// 获取url参数
  Map<String, String> getUrlParams() {
    Map<String, String> params = {};
    if (this == null) {
      return params;
    }
    int index = this!.indexOf("?");
    if (index == -1) {
      return params;
    }
    String url = this!.substring(index + 1);
    List<String> list = url.split("&");
    for (var item in list) {
      List<String> param = item.split("=");
      params[param[0]] = param[1];
    }
    return params;
  }

  /// 添加参数
  String? addUrlParams(dynamic params) {
    if (this != null && params != null) {
      try {
        String str = this!;
        params.forEach((key, value) {
          if (!str.contains("?")) {
            str = "$str?$key=$value";
          } else {
            str = "$str&$key=$value";
          }
        });
        return str;
      }catch(e){
        return this;
      }
    }
    return this;
  }

  /// 移除url参数
  String? removeUrlParams() {
    if (this != null && this!.contains("?") == true) {
      return this!.substring(0, this!.indexOf("?"));
    }
    return this;
  }

  String formatRelaTime() {
    DateTime dateTime;
    if(this==null || this==''){
      return '--';
    }
    try {
      dateTime=DateTime.parse(this!);
    } catch (e) {
      return '--';
    }
    String hourStr = dateTime.hour.toString().padLeft(2, '0');
    String minuteStr = dateTime.minute.toString().padLeft(2, '0');
    String period = dateTime.hour < 12 ? '上午' : '下午';
    DateTime today = DateTime.now();
    DateTime tomorrow=today.add(const Duration(days: 1));
    DateTime dayAfterTomorrow=today.add(const Duration(days: 2));
    String dateStr;
    if(dateTime.year == today.year && dateTime.month == today.month && dateTime.day == today.day){
      dateStr = '今天';
    } else if(dateTime.year == tomorrow.year && dateTime.month == tomorrow.month && dateTime.day == tomorrow.day){
      dateStr = '明天';
    }else if(dateTime.year == dayAfterTomorrow.year && dateTime.month == dayAfterTomorrow.month && dateTime.day == dayAfterTomorrow.day){
      dateStr = '后天';
    } else {
      return '${dateTime.year}/${dateTime.month}/${dateTime.day} $hourStr:$minuteStr 装货';
    }
    return "$dateStr $period $hourStr:$minuteStr 装货";
  }

  String formatMMdd() {
    DateTime dateTime;
    if(this==null || this==''){
      return '--';
    }
    try {
      dateTime=DateTime.parse(this!);
    } catch (e) {
      return '--';
    }
    String hourStr = dateTime.hour.toString().padLeft(2, '0');
    String minuteStr = dateTime.minute.toString().padLeft(2, '0');
    return '${dateTime.month}月${dateTime.day}日 $hourStr:$minuteStr 装货';
  }

}
